﻿Imports TBCC.UtilityMobile
Public Class PortItemService

    Public Sub New(ByVal pi As ProjectItem)
        _pi = pi
    End Sub

    Private _pi As ProjectItem = Nothing

    ''' <summary>
    ''' 增加一行circuitItem
    ''' </summary>
    ''' <param name="cci"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Create(ByVal cci As PortItem) As Integer
        Dim strSql As String
        strSql = "Insert Into tbccCircuit(ccName,listNo,NetAddress,Remark,projectID,dateCreated,line,ccID) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7)"

        Dim p(7) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = cci.ccName
        p(1).Value = cci.listNo
        p(2).Value = cci.NetAddress
        p(3).Value = cci.remark
        p(4).Value = cci.projectID
        p(5).Value = cci.dateCreated
        p(6).Value = cci.line
        p(7).Value = cci.ccID

        Return SqlHelper.ExecuteNonQuery(strSql, p)

    End Function

    ''' <summary>
    ''' 修改circuitItem
    ''' </summary>
    ''' <param name="cci"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Update(ByVal cci As PortItem) As Integer
        Dim strSql As String
        strSql = "Update tbccCircuit Set ccName=?p0,listNo=?p1,NetAddress=?p2,Remark=?p3,line=?p4 where ccID=?p5"

        Dim p(5) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = cci.ccName
        p(1).Value = cci.listNo
        p(2).Value = cci.NetAddress
        p(3).Value = cci.remark
        p(4).Value = cci.line
        p(5).Value = cci.ccID


        Return SqlHelper.ExecuteNonQuery(strSql, p)
    End Function

    ''' <summary>
    ''' 删除指定的circuitItem
    ''' </summary>
    ''' <param name="cci"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Delete(ByVal cci As PortItem) As Boolean
        Dim strSql As String
        strSql = "Delete From tbccCircuit Where ccID=?p0"
        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(0).ParameterName = "p0" : p(0).Value = cci.ccID

        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        tic.Add(strSql, p)

        '主工程图中的动态配置的控件
        strSql = "Delete From tbccImageControls Where DataID=?p0"
        tic.Add(strSql, p)


        Return TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)
        'Return SqlHelper.ExecuteNonQuery(strSql, p)
    End Function

    ''' <summary>
    ''' 读取circuitItem列表
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetList() As IList(Of PortItem)
        Dim l As IList(Of PortItem)
        l = New List(Of PortItem)

        Dim strSql As String
        strSql = "Select * From tbccCircuit Where projectID=?p0 Order by listNo"
        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(0).ParameterName = "p0" : p(0).Value = _pi.ProjectID

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, p)

        For Each r As DataRow In ds.Tables(0).Rows
            Dim cci As New PortItem()

            cci.ccID = r("ccID")
            cci.ccName = r("ccName")

            cci.projectID = r("projectID")
            cci.listNo = r("listNo")
            cci.NetAddress = r("netAddress")
            cci.dateCreated = r("DateCreated")
            cci.remark = r("remark")
            cci.line = r("line")

            l.Add(cci)
        Next

        Return l
    End Function

    ''' <summary>
    ''' 读取一条circuitItem记录
    ''' </summary>
    ''' <param name="ccID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetModel(ByVal ccID As String) As PortItem
        Dim strSql As String
        strSql = "Select * From tbccCircuit Where ccID=?p0 "
        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(0).ParameterName = "p0" : p(0).Value = ccID

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, p)

        Dim cci As New PortItem()

        If ds.Tables.Count <> 1 OrElse ds.Tables(0).Rows.Count <> 1 Then
            Return Nothing
        End If

        Dim r As DataRow
        r = ds.Tables(0).Rows(0)

        cci.ccID = r("ccID")
        cci.ccName = r("ccName")

        cci.projectID = r("projectID")
        cci.listNo = r("listNo")
        cci.NetAddress = r("netAddress")
        cci.dateCreated = r("DateCreated")
        cci.remark = r("remark")
        cci.line = r("line")

        Return cci
    End Function

    ''' <summary>
    ''' 判断工程中是否包含指定的网络地址
    ''' </summary>
    ''' <param name="netID"></param>
    ''' <param name="line"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ExistNetaddress(ByVal netID As Byte, ByVal line As Integer) As Boolean
        Dim strSql As String
        strSql = "Select count(*) from [tbccCompressorGroup] a where projectID=?p0 and netaddress =?p1"

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(1) = New MySql.Data.MySqlClient.MySqlParameter

        p(0).ParameterName = "p0" : p(0).Value = Me._pi.ProjectID
        p(1).ParameterName = "p1" : p(1).Value = netID

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Throw New Exception(String.Format("网络地址{0}已存在", netID))
        End If

        If obj > 0 Then
            Return True
        End If

        strSql = "Select count(*) from [tbccCircuit] a where projectID=?p0 and netaddress =?p1"
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Throw New Exception(String.Format("网络地址{0}已存在", netID))
        End If

        Return (obj > 0)
    End Function


End Class
